public class Compare {
    public static void main(String[] args) {
        int runnum = 100000;
        System.out.println("运行" + runnum + "次：");
        String a = "";
        long tm1 = System.currentTimeMillis();
        for (int i = 0; i < runnum; i++)
            a += "a";
        long tm2 = System.currentTimeMillis();
        System.out.println(("普通str连接用时：" + (tm2 - tm1) + "毫秒"));

        StringBuilder sb = new StringBuilder();
        tm1 = System.currentTimeMillis();
        for (int i = 0; i < runnum; i++)
            sb.append("a");
        tm2 = System.currentTimeMillis();
        System.out.println(("stringbuilder和append连接用时：" + (tm2 - tm1) + "毫秒"));

        StringBuffer sf = new StringBuffer();
        tm1 = System.currentTimeMillis();
        for (int i = 0; i < runnum; i++)
            sf.append("1");
        tm2 = System.currentTimeMillis();
        System.out.println(("stringbuffer和append连接用时：" + (tm2 - tm1) + "毫秒"));
    }
}
